class Solution {
public:
    string longestPalindrome(string s)
    {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n, 0));
        string ans;
        for (int i = 0; i < n; i++)
        {
            for (int j = i; j >= 0; j--)
            {
                if (s[i] != s[j]) continue;
                else
                {
                    if (i == j || j + 1 == i) dp[i][j] = 1;
                    else dp[i][j] = dp[i - 1][j + 1];
                }
                if (dp[i][j] && i - j + 1 > ans.size())
                {
                    ans = string(s.begin() + j, s.begin() + i + 1);
                }
            }
        }
        return ans;
    }
};